package com.atguigu.gmall.order.config;

import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @Auther 赵乾坤
 * @Date 2021-01-11 12:56
订单 取消相关
 * <bean class
 * @Au  java配置类
 */


@Configuration
public class DeadLetterMQConfig {
    /**
     * 取消订单，发送延迟队列
     * 死信 一个交换机 两个routingkey 二个队列
     */
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "exchange.direct.order.cancel";//"exchange.direct.order.create" test_exchange;
    public static final String ROUTING_ORDER_CANCEL1 = "order.create1";
    public static final String ROUTING_ORDER_CANCEL2 = "order.create2";
    //延迟取消订单队列
    public static final String QUEUE_ORDER_CANCEL1  = "queue.order.cancel1";
    public static final String QUEUE_ORDER_CANCEL2  = "queue.order.cancel2";
    //取消订单 延迟时间 单位：秒
    public static final int DELAY_TIME  = 2*60*60*1000;//全局设置


    //TTL队列
    @Bean
    public Queue queueOrderCancel1() {
        Map<String, Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", EXCHANGE_DIRECT_ORDER_CANCEL);
        arguments.put("x-dead-letter-routing-key", ROUTING_ORDER_CANCEL2);
        arguments.put("x-message-ttl", DELAY_TIME);
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL1).withArguments(arguments).build();


    }

    //普通队列
    @Bean
    public Queue queueOrderCancel2() {
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL2).build();
    }


    //交换机（普通）
    @Bean
    public Exchange exchangeOrderCancel() {
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_ORDER_CANCEL).build();
    }

    //绑定2遍
    @Bean
    public Binding bindingOrderCancel1(@Qualifier(value = "queueOrderCancel1") Queue qc1
            , @Qualifier(value = "exchangeOrderCancel") Exchange eoc) {
        return BindingBuilder.bind(qc1).to(eoc).with(ROUTING_ORDER_CANCEL1).noargs();
    }

    @Bean
    public Binding bindingOrderCancel2(@Qualifier(value = "queueOrderCancel2") Queue qc2
            , @Qualifier(value = "exchangeOrderCancel") Exchange eoc) {
        return BindingBuilder.bind(qc2).to(eoc).with(ROUTING_ORDER_CANCEL2).noargs();
    }
}
